package com.shop.user.api.service.impl;

import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.user.AddressInput;
import com.shop.common.model.result.user.AddressOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.common.util.state.AddressState;
import com.shop.user.api.service.IAddressSvc;
import com.shop.user.atom.atomsvc.IAddressAtomSvc;
import com.shop.user.conf.util.DataState;
import com.shop.user.conf.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhoulk
 * @Description:
 * @date: 2019年03月26日  16:51
 */
@Service
public class AddressSvc extends BaseService implements IAddressSvc {
    final static Logger logger = LoggerFactory.getLogger(AddressSvc.class);
    @Autowired
    IAddressAtomSvc addressAtomSvc;

    @Override
    public ServiceBean<String> insertAddress(InputBean<AddressInput> inputBean) {
        AddressInput address = inputBean.getParam();
        Integer count = addressAtomSvc.selectAddressCount(inputBean.getParam());
        if (count != null && count > 10) {
            logger.info("用户【ID】:" + inputBean.getParam().getUserId() + "新建地址失败");
            logger.info("用户【ID】：" + inputBean.getParam().getUserId() + "【失败原因】:地址数量达到最大数量");
            return resultFailed("地址数量以达到最大限制");
        }
        if (count == null || count == 0) {
            address.setStatus(AddressState.USED.value());
        } else {
            address.setStatus(AddressState.WAIT.value());
        }
        address.setCreateTime(DateUtil.newNowDate());
        address.setStatus(AddressState.WAIT.value());
        address.setDataState(DataState.Normal.getValue());
        ServiceBean serviceBean = addressAtomSvc.insertAddress(address);
        return serviceBean;
    }

    @Override
    public ServiceBean<AddressOutput> selectAddress(InputBean<AddressInput> inputBean) {
        return addressAtomSvc.selectAddress(inputBean.getParam());
    }

    @Override
    public ServiceBean<List<AddressOutput>> selectAddressList(InputBean<AddressInput> inputBean) {
        return addressAtomSvc.selectAddressList(inputBean.getParam());
    }

    @Override
    public ServiceBean<String> updateAddress(InputBean<AddressInput> inputBean) {
        return addressAtomSvc.updateAddress(inputBean.getParam());
    }

    @Override
    public ServiceBean<String> deleteAddressById(InputBean<List<Integer>> inputBean) {
        AddressInput addressInput = new AddressInput();
        addressInput.setId(inputBean.getParam().get(0));
        ServiceBean<AddressOutput> serviceBean = addressAtomSvc.selectAddress(addressInput);
        if (serviceBean.isSuccess() && serviceBean.getData().getStatus().intValue() == AddressState.USED.value()) {
            return resultFailed("默认地址不能删除");
        }
        return addressAtomSvc.deleteAddressById(inputBean.getParam());
    }

    @Override
    public ServiceBean<String> deleteAddressByUser(InputBean<Integer> inputBean) {
        return null;
    }

    @Override
    public ServiceBean settingDefault(AddressInput address) {
        AddressInput address_t = new AddressInput();
        address_t.setUserId(address.getUserId());
        address_t.setStatus(AddressState.USED.value());
        ServiceBean<AddressOutput> serviceBean = addressAtomSvc.selectAddress(address_t);
        if (serviceBean.isSuccess()) {
            AddressOutput addressOutput = serviceBean.getData();
            if (addressOutput.getId().intValue() == address.getId().intValue()) {
                return resultFailed("该地址已经是默认地址，无需修改");
            }
            address_t.setId(addressOutput.getId());
            address_t.setStatus(AddressState.WAIT.value());
            addressAtomSvc.updateAddress(address_t);
        }
        address.setStatus(AddressState.USED.value());
        return addressAtomSvc.updateAddress(address);
    }


}
